import os
import glob
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

y0 = []
name = []
for file_name in glob.glob(os.path.join(os.getcwd(), '*.txt')):
    with open(file_name, "r+") as readingfile:
        rows = readingfile.readlines()
        x = []
        y = []
        for row in rows:
            if row[0] != '#':
                m = row.strip('\n').split(' ')
                m.pop()
                for n in m:
                    xy = n.split(',')
                    x.append(xy[0])
                    y.append(xy[1])
        newname = file_name.split('\\')[-1].strip('.txt')
        ylist = list(map(float, y))
        ymax = max(ylist)
        ymin = min(ylist)
        y0.append(y)
        name.append(newname)
        plt.figure(figsize=(22, 10))  # 设置画布为2200*1000像素
        plt.xticks([xt for xt in np.arange(0, 800, 50)])  # x轴刻度为50，长为800
        plt.yticks([yt for yt in np.arange(ymin, ymax , round((ymax - ymin) / 15, 5))])  # 自动坐标轴
        plt.plot(list(map(float, x)), ylist)
        plt.savefig(newname + '.jpg')
df = pd.DataFrame({})
df['x'] = x
for n in range(len(y0)):
    df[name[n]] = pd.Series(y0[n])
writer = pd.ExcelWriter('guangpu' + '.xlsx')
df.T.to_excel(writer, newname, index=False)  # 只保存column而没有index
writer.save()
